package com.jerei.web.utils.spring;

import com.jerei.web.utils.UtilsPluginManager;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import ro.fortsoft.pf4j.PluginWrapper;

import java.util.*;

/**
 * Created by sunfeihu on 2016/11/30.
 */
public class JrResourceBundleMessageSource extends ReloadableResourceBundleMessageSource {

    private static Set<String> beanNameSet = new HashSet<String>();

    private String[] oldbeannames = null;

    public static boolean update = false;

    @Override
    protected String resolveCodeWithoutArguments(String code, Locale locale) {
        //如果需要更新
        if (update) {
            update = false;
            this.clearCache();
            this.setBasenames(oldbeannames);
            for (String basename : getBasenameSet()) {
                List<String> filenames = calculateAllFilenames(basename, locale);
                for (String filename : filenames) {
                    PropertiesHolder propHolder = getProperties(filename);
                    String result = propHolder.getProperty(code);
                    if (result != null) {
                        return result;
                    }
                }
            }
        }
        if (getCacheMillis() < 0) {
            PropertiesHolder propHolder = getMergedProperties(locale);
            String result = propHolder.getProperty(code);
            if (result != null) {
                return result;
            }
        } else {
            for (String basename : getBasenameSet()) {
                List<String> filenames = calculateAllFilenames(basename, locale);
                for (String filename : filenames) {
                    PropertiesHolder propHolder = getProperties(filename);
                    String result = propHolder.getProperty(code);
                    if (result != null) {
                        return result;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public void setBasenames(String... basenames) {
        oldbeannames = basenames;
        beanNameSet.clear();
        if (null != oldbeannames) {
            beanNameSet.addAll(Arrays.asList(oldbeannames));
        }
        this.updatePluginI18n();
        String[] bArr = new String[beanNameSet.size()];
        super.setBasenames(beanNameSet.toArray(bArr));
    }

    private void updatePluginI18n() {
        String[] langs = new String[]{"de", "en", "es", "fr", "it", "ko", "pt", "tw", "zh"};
        if (null != UtilsPluginManager.get()) {
            //获取插件，并添加插件内的国际化文件
            List<PluginWrapper> pluginWrappers = UtilsPluginManager.get().getPlugins();
            if (null != pluginWrappers && pluginWrappers.size() > 0) {
                for (PluginWrapper pluginWrapper : pluginWrappers) {
                    for (int i = 0; i < langs.length; i++) {
                        beanNameSet.add("/admin/plugins" + pluginWrapper.getPluginPath() + "/classes/i18n/" + langs[i] + "/1");
                    }
                }
            }
        }

    }
}